//
//  PlayeViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/18.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "PlayeViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "KrVideoPlayerController.h"
#import "FMGVideoPlayView.h"
#import "FullViewController.h"
@interface PlayeViewController ()

@property(nonatomic, strong)AVPlayer *videoPlayer;
@property(nonatomic, strong)AVPlayerLayer *layer;
@property(nonatomic, strong)UIView *myView;

@property (nonatomic, strong) KrVideoPlayerController  *videoController;
@end

@implementation PlayeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor blackColor];
    
    [self creatBo];
    

    // Do any additional setup after loading the view.
}
-(void)creatBo{
    
    NSURL *url = [NSURL URLWithString:_model.mp4_url];
    
    //1.使用自定义播放器KrVideoPlayerController
   [self addVideoPlayerWithURL:url];
    
    
//    //2.使用AV播放器
//    //构造AVPlayerItem对象
//    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
//    //初始化player
//    self.videoPlayer = [[AVPlayer alloc] initWithPlayerItem:item];
//    //到这里还没有画面,我们还需要一个AVPlayerLayer对象
//    self.layer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
//    self.layer.frame = CGRectMake(0, 100, self.view.frame.size.width, 500);
//    self.layer.videoGravity = AVLayerVideoGravityResizeAspect;//让其居中显示
//    //添加到屏幕上显示
//    [self.view.layer addSublayer:self.layer];
//    
//    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 500)];
//
//    [self.view addSubview:self.myView];
//    
//    
//     [self.videoPlayer play];
    
    
//    //第三种播放器FMGVideoPlayView
//    FMGVideoPlayView *playView = [FMGVideoPlayView videoPlayView];
//    // 视频资源路径
//    [playView setUrlString:_model.mp4_url];
//    // 播放器显示位置（竖屏时）
//    playView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width * 9 / 16);
//    // 添加到当前控制器的view上
//    [self.view addSubview:playView];
//    
//    // 指定一个作为播放的控制器
//    playView.contrainerViewController = self;
    
    
    
    
    
    
    
    
    
    //返回按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //leftButton.backgroundColor = [UIColor whiteColor];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.videoController.view addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(20);
        
    }];
    [leftButton addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
    
   
}

-(void)leftAction{
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}




















-(void)setModel:(videoModel *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    _model = model;
//    self.str = _model.mp4_url;
}
















//1.使用自定义播放器KrVideoPlayerController
- (void)addVideoPlayerWithURL:(NSURL *)url{
    if (!self.videoController) {
        CGFloat width = [UIScreen mainScreen].bounds.size.width;
        self.videoController = [[KrVideoPlayerController alloc] initWithFrame:CGRectMake(0, 64, width, width*(9.0/16.0))];
        __block typeof(self)weakSelf = self;
        [self.videoController setDimissCompleteBlock:^{
            weakSelf.videoController = nil;
        }];
//        [self.videoController setWillBackOrientationPortrait:^{
//            [weakSelf toolbarHidden:NO];
//        }];
//        [self.videoController setWillChangeToFullscreenMode:^{
//            [weakSelf toolbarHidden:YES];
//        }];
        [self.view addSubview:self.videoController.view];
    }
    self.videoController.contentURL = url;

}
//隐藏navigation tabbar 电池栏
- (void)toolbarHidden:(BOOL)Bool{
    self.navigationController.navigationBar.hidden = Bool;
    self.tabBarController.tabBar.hidden = Bool;
    [[UIApplication sharedApplication] setStatusBarHidden:Bool withAnimation:UIStatusBarAnimationFade];
}
@end
